import { _decorator, easing, sys, SpriteFrame, UITransformComponent, Node, Color, math, Sprite, SystemEvent, LabelComponent, SkinnedMeshRenderer, Prefab, v3, Vec3, SkeletalAnimation, ProgressBar, tween, repeat } from 'cc';
import { sp } from "cc";
const { Skeleton } = sp;
import BaseView from '../../../Script/game/BaseView';
// import { luckyBox } from './luckyBox';
import GD from '../../../Script/config/GD';
import GDef from '../../../Script/config/GDef';
import Tool from '../../../Script/tool/Tool'
import SdkMgr from '../../../Script/tyq3.x/SdkMgr';
import TyqEventMgr from '../../../Script/tyq3.x/tyq-event-mgr';
const { bits } = math;
const { ccclass, property } = _decorator;

@ccclass('preManghe')
export class preManghe extends BaseView {
    @property({
        type: Node
    })
    nodeManghe: Node = null;
    @property({
        type: Node
    })
    nodeTitle: Node = null;
    @property({
        type: Node
    })
    nodeLight: Node = null;
    @property({
        type: Node
    })
    btnOpen: Node = null;
    @property({
        type: Node
    })
    btnGiveup: Node = null;
    @property({
        type: SpriteFrame
    })
    sprBtn: SpriteFrame = null;
    private nodeDino: Node = null;
    private countRetry: number = 0;
    private intStat: number = 0;

    onLoad() {
        var self = this;
        GDef.getCameraTop().node.setPosition(v3(0, 2, 6));
        GDef.getCameraTop().node.setRotationFromEuler(v3(-30, 0, 0)),

            //再来一次
            this.bindButton(this.btnOpen, () => {
                TyqEventMgr.ins.sendEvent('观看激励视频-盲盒再开一次');
                SdkMgr.ins.showRewardAd("观看激励视频-盲盒再开一次", () => {
                    TyqEventMgr.ins.sendEvent('观看完激励视频-盲盒再开一次');
                    self.countRetry++;
                    self.playMangheDrop();
                });
            });
        //继续游戏
        this.bindButtonOce(this.btnGiveup, () => {
            if (this.tweenBtn0) this.tweenBtn0.stop();
            if (this.tweenBtn1) this.tweenBtn1.stop();
            this.emit(GDef.wdEvent.closeManghe);
            this.closeOrOpen(0);
            this.nodeManghe.getComponent(Skeleton).enabled = false;
            if (this.nodeDino) {
                GDef.poolArm.put(this.nodeDino);
                this.nodeDino = null;
            }
            if (this.tween) this.tween.stop();
            tween(this.node).delay(0.1).call(() => {
                this.node.destroy();
            }).start();
        });
        tween(this.nodeLight).by(6, { eulerAngles: v3(0, 0, -360) }).union().repeatForever().start();


        this.nodeManghe.getComponent(Skeleton).loop = false;
        this.nodeManghe.getComponent(Skeleton).premultipliedAlpha = false;
        this.nodeManghe.getComponent(Skeleton).setCompleteListener((x: any) => {
            if (this.intStat == 0) {
                this.btnOpen.scale = v3(0, 0, 0);
                this.btnGiveup.scale = v3(0, 0, 0);
                this.intStat = 1;
                this.closeOrOpen(1);
                this.nodeManghe.getComponent(Skeleton).setAnimation(0, 'standBy', true);
                this.nodeManghe.getComponent(Skeleton).loop = true;
                this.bindButtonOce(this.nodeManghe, () => {
                    //看視頻得恐龍
                    GD.addPlayerArm(this.jsonArmType.armType, this.jsonArm.Tid);

                    this.intStat = 2;
                    this.nodeManghe.getComponent(Skeleton).setAnimation(0, 'open', false);
                    this.nodeManghe.getComponent(Skeleton).loop = false;
                    this.emit(GDef.wdEvent.openManghe);
                    tween(this.nodeDino).delay(2.1).call(() => {
                        Tool.playPD(GDef.nodeRoot);
                        this.emit(GDef.wdEvent.playEffect, "Upgrade");
                        if (sys.platform === sys.Platform.WECHAT_GAME && wx.vibrateShort) wx.vibrateShort({ type: 'light', style: 'light', fail() { } })
                    }).to(0.6, { scale: v3(1, 1, 1) }, { easing: easing.elasticOut }).start();
                });
            }
            else if (this.intStat == 2) {
                this.showBtn();
            }
        });
    }
    createDino() {
        //生成一只恐龙
        if (this.nodeDino) {
            GDef.poolArm.put(this.nodeDino);
            this.nodeDino = null;
        }
        console.log("生成一只恐龙");
        this.jsonArmType = jsonTables.getJsonTableObj("dinoType", this.lv);
        if (!this.jsonArmType) return;
        this.jsonArm = jsonTables.getJsonTableObj("dino", this.lv * 2 - 1 + Math.min(1, this.countRetry));
        if (!this.jsonArm) return;
        Tool.initArm(1, GDef.nodeArms, GDef.SidePlayer, v3(0, 0, 2), v3(0, 180, 0), this.jsonArmType, this.jsonArm, 0, GDef.LayerUI3D, 2.5).then((node) => {
            this.nodeDino = node;
            this.tween = tween(node).by(12, { eulerAngles: v3(0, 360, 0) }).union().repeatForever();
            this.tween.start();
            this.nodeDino.scale = v3(0, 0, 0);
        });
    }
    startPlay() {

        //审核开关
        if (Tool.isInVerify()) {
            this.btnGiveup.getComponent(Sprite).spriteFrame = this.sprBtn;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnOpen.getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnOpen.getComponent(UITransformComponent).contentSize.height;
        }
        else {
            this.btnGiveup.getComponent(Sprite).spriteFrame = null;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.width = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.width;
            this.btnGiveup.getComponent(UITransformComponent).contentSize.height = this.btnGiveup.children[0].getComponent(UITransformComponent).contentSize.height;
        }

        Tool.hideBanner();
        this.lv = GD.getMangHeDinoLv()
        this.countRetry = 0;
        this.playMangheDrop();
    }
    playMangheDrop() {
        this.emit(GDef.wdEvent.playEffect, "Upgrade");
        this.intStat = 0;
        if (this.nodeDino) {
            GDef.poolArm.put(this.nodeDino);
            this.nodeDino = null;
        }
        if (this.tween) this.tween.stop();
        this.createDino();
        this.nodeManghe.getComponent(Skeleton).enabled = true;
        this.nodeManghe.getComponent(Skeleton).setAnimation(0, 'drop', false);
        this.scheduleOnce(() => {
            if (this.tweenBtn0) this.tweenBtn0.stop();
            if (this.tweenBtn1) this.tweenBtn1.stop();
            this.btnOpen.scale = v3(0, 0, 0);
            this.btnGiveup.scale = v3(0, 0, 0);
        }, 0.1);
    }
    closeOrOpen(bol: number) {
        tween(this.nodeTitle).to(0.1, { scale: v3(bol, bol, bol) }).start();
        tween(this.nodeManghe).to(0.1, { scale: v3(bol, bol, bol) }).start();
        tween(this.nodeLight).to(0.1, { scale: v3(bol, bol, bol) }).start();
        if (bol == 0) {
            tween(this.btnOpen).to(0.1, { scale: v3(bol, bol, bol) }).start();
            tween(this.btnGiveup).to(0.1, { scale: v3(bol, bol, bol) }).start();
        }
    }
    //显示按钮
    showBtn() {
        if (this.countRetry == 0) {
            this.tweenBtn0 = tween(this.btnOpen).to(0.2, { scale: v3(1, 1, 1) }, { easing: easing.elasticOut });
            this.tweenBtn0.start();
        }
        //审核开关
        let delay = 1.5;
        if (Tool.isInVerify()) delay = 0;
        this.tweenBtn1 = tween(this.btnGiveup).delay(delay).to(0.2, { scale: v3(1, 1, 1) });
        this.tweenBtn1.start();
    }
    onDisable() {
    }
}
